﻿using System;
using System.Windows.Input;
using tnet.Controls.Lookless;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Devices;
using Microsoft.Advertising.Mobile.UI;

namespace tnet
{
    public partial class TempNewsItem : PhoneApplicationPage
    {
        public String FreezeDriedNewItem = String.Empty;
        public String[] NewItemArray;
        private VibrateController _Vibration = VibrateController.Default;
        private TimeSpan _ThisTime = new TimeSpan(0, 0, 0, 0, 30);
        private String _HTMLPrefix = "<a href='http://transhumanity.net'><small><b>transhumanity.net</b></small></a><br /><h3>";
        private String _EmailSubjectPrefix = "transhumanity.net for WP: ";
        private String TwitterHandle = "DavidJKelley";

        public TempNewsItem()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            try
            {
                AdControl.TestMode = false;

                FreezeDriedNewItem = Tools.GetSetting("TempNewsItem").ToString();
                NewItemArray = FreezeDriedNewItem.Split('^');

                _HTMLPrefix += "<a href='" + NewItemArray[1] + "'>" + NewItemArray[0] + "</a></h3><br />";

                webBrowser1.NavigateToString(_HTMLPrefix + NewItemArray[3]);
            }
            catch (Exception) 
            {
                _HTMLPrefix += "<a href='" + FreezeDriedNewItem + "'>" + FreezeDriedNewItem + "</a></h3><br />";
                webBrowser1.NavigateToString(_HTMLPrefix + "<br />[ no data connection ]");
            }
        }
           
        private void RateIcn_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime);

            MarketplaceReviewTask ThisItem = new MarketplaceReviewTask();
            ThisItem.Show();
        }

        private void EmailIcn_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime);

            Tools.SendEmail(NewItemArray[1], _EmailSubjectPrefix + NewItemArray[0]); 
        }

        private void HomeIcn_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime);

            NavigationService.GoBack(); 
        }

        private void AddToFavorites_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime);

            String KeyValue = Tools.ScrubTitleToKey(NewItemArray[0]) + ".fav";

            String Temp = Tools.GetSetting("FavoritesList.index").ToString();
            if (String.IsNullOrEmpty(Temp))
            {
                Tools.SaveSetting("FavoritesList.index", KeyValue);
            }
            else
            {
                if (Temp.IndexOf(KeyValue) == -1)
                {
                    Temp += "^" + KeyValue;
                    Tools.SaveSetting("FavoritesList.index", Temp);
                }
            } 
        }

        private void Facebook_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime);

            Tools.SendToFacebook(NewItemArray[1], NewItemArray[0]);
        }

        private void Tweet_Click(object sender, EventArgs e)
        {
            _Vibration.Start(_ThisTime);
             
            Tools.SendTwitter(NewItemArray[1], NewItemArray[0], TwitterHandle); 
        }
    }
}